//package com.plian.system.service.dg.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.plian.Tools.*;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.entity.dg.EnterpriseAssociation;
//import com.plian.system.entity.pm.property.PropertyBasics;
//import com.plian.system.entity.pm.property.PropertyRecord;
//import com.plian.system.mapper.dg.EnterpriseAssociationMapper;
//import com.plian.system.mapper.pm.property.PropertyBasicsMapper;
//import com.plian.system.mapper.pm.property.PropertyRecordMapper;
//import com.plian.system.model.CommonCode;
//import com.plian.system.model.ListResult;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.mybatis.search.SearchEntity;
//import com.plian.system.mybatis.search.SearchParamEntiy;
//import com.plian.system.mybatis.support.Condition;
//import com.plian.system.mybatis.support.Query;
//import com.plian.system.service.dg.IEnterpriseAssociationService;
//import com.plian.system.service.sys.file.FileService;
//import com.plian.system.vo.dg.EnterpriseAssociationVO;
//import com.plian.system.wrapper.dg.EnterpriseAssociationWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.*;
//import java.util.stream.Collectors;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//import static com.plian.system.constant.sys.FormTypeConstant.DG_ENTERPRISE_ASSOCIATION;
//
///**
// * @Description: TODO
// * @Author: Take-off
// * @Date: 2023/4/4 4:26 PM
// * @Param
// * @return
// * @Version: 1.0
// **/
//@Service
//@AllArgsConstructor
//@Slf4j
//public class EnterpriseAssociationServiceImpl extends BaseServiceImpl<EnterpriseAssociationMapper, EnterpriseAssociation> implements IEnterpriseAssociationService, BaseFlowService {
//
//    private final EnterpriseAssociationWrapper enterpriseAssociationWrapper;
//
//    private final FileService fileService;
//
//    private final PropertyRecordMapper recordMapper;
//
//    private final PropertyBasicsMapper propertyBasicsMapper;
//    @Override
//    public boolean saveOrUpdate(EnterpriseAssociation enterpriseAssociation) {
//        boolean flag = false;
//        if (!Optional.ofNullable(enterpriseAssociation.getId()).isPresent()) {
//            enterpriseAssociation.setVersion(1);
//            enterpriseAssociation.setFormType(String.valueOf(FormTypeConstant.DG_ENTERPRISE_ASSOCIATION));
//            enterpriseAssociation.setAssociationStatus(1);
//            enterpriseAssociation.setFileStatus(1);
//            flag = true;
//        }
//        boolean result = super.saveOrUpdate(enterpriseAssociation);
//        if (flag){
//            fileService.saveAll(String.valueOf(enterpriseAssociation.getId()), enterpriseAssociation.getDocList());
//        }else {
//            fileService.edit(String.valueOf(enterpriseAssociation.getId()), enterpriseAssociation.getDocList());
//        }
//        return result;
//    }
//
//    @Override
//    @Transactional(rollbackFor=RuntimeException.class)
//    public boolean submit(EnterpriseAssociation enterpriseAssociation) {
//        boolean result = saveOrUpdate(enterpriseAssociation);
//        return result;
//    }
//
//    @Override
//    public ListResult<EnterpriseAssociationVO> versionList(Query query) {
//        List<SearchEntity> entityList = query.getSearch();
//        if (query.getAscs()==null&&query.getDescs()==null){
//            query.setDescs("version");
//        }
//        QueryWrapper<EnterpriseAssociation> queryWrapper = SearchParamEntiy.getQueryWrapper(
//                Condition.getQueryWrapper(new EnterpriseAssociation()), entityList
//        );
//        IPage<EnterpriseAssociation> pages = page(Condition.getPage(query), queryWrapper);
//        List<EnterpriseAssociationVO> enterpriseAssociationVOS = enterpriseAssociationWrapper.entityToVO(pages.getRecords());
//        ListResult<EnterpriseAssociationVO> listResult = new ListResult<>(CommonCode.SUCCESS);
//        listResult.setList(enterpriseAssociationVOS);
//        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
//        return listResult;
//    }
//
//
//    @Override
//    public ListResult<EnterpriseAssociationVO> list(Query query) {
//        List<SearchEntity> entityList = query.getSearch();
//        if (query.getAscs()==null&&query.getDescs()==null){
//            query.setDescs("create_time");
//        }
//        QueryWrapper<EnterpriseAssociation> queryWrapper = SearchParamEntiy.getQueryWrapper(
//                Condition.getQueryWrapper(new EnterpriseAssociation()), entityList
//        );
//        String keyword = query.getKeyword();
//        //获取关联产权
//        List<String> proIds = new ArrayList<>();
//         String orgId = TokenUtil.getTokenOrgId();
//        PropertyBasics basics = propertyBasicsMapper.selectOne(new LambdaQueryWrapper<PropertyBasics>().eq(PropertyBasics::getOrgId, orgId));
//        if (Optional.ofNullable(basics).isPresent()){
//            //所有子集
//            List<PropertyBasics> propertyBasics = propertyBasicsMapper.selectList(new LambdaQueryWrapper<PropertyBasics>().likeRight(PropertyBasics::getPropertyCode, basics.getPropertyCode())
//                    .like(Optional.ofNullable(keyword).isPresent(), PropertyBasics::getOrgName, keyword));
//            if (CollectionUtil.isNotEmpty(propertyBasics)){
//                List<PropertyRecord> recordList = recordMapper.selectList(new LambdaQueryWrapper<PropertyRecord>().in(PropertyRecord::getInitialId, propertyBasics.stream().map(PropertyBasics::getId).collect(Collectors.toList())));
//                if (CollectionUtil.isNotEmpty(recordList)){
//                    proIds = recordList.stream().map(PropertyRecord::getId).collect(Collectors.toList());
//                }
//            }
//        }
//        if (CollectionUtil.isNotEmpty(proIds)){
//            queryWrapper.in("pro_id",proIds);
//        }else {
//            queryWrapper.eq("id","-1");
//        }
//
//        IPage<EnterpriseAssociation> pages = page(Condition.getPage(query), queryWrapper);
//        List<EnterpriseAssociationVO> enterpriseAssociationVOS = enterpriseAssociationWrapper.entityToVO(pages.getRecords());
//        ListResult<EnterpriseAssociationVO> listResult = new ListResult<>(CommonCode.SUCCESS);
//        listResult.setList(enterpriseAssociationVOS);
//        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
//        return listResult;
//    }
//
//    @Override
//    public HashMap<String, Object> commit(String id) {
//        handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//        //将需要提交的流程变量返回，用于工作流侧
//        return new HashMap<String, Object>();
//    }
//
//    @Override
//    public Boolean finish(String id) {
//        return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//    }
//
//    @Override
//    public Boolean reject(String id, int formState, String comment) {
//        return handleStatus(Long.valueOf(id), formState);
//    }
//
//    @Override
//    public void goBackToStash(String formId) {
//        handleStatus(Long.valueOf(formId), UNREVIEWED);
//    }
//
//    @Override
//    public List<PageData> findByIds(List<String> formIds) {
//        List<EnterpriseAssociation> enterpriseAssociations = list(new LambdaQueryWrapper<EnterpriseAssociation>()
//                .in(EnterpriseAssociation::getId, Func.toLongList(StringUtils.join(formIds, StringPool.COMMA))));
//        List<EnterpriseAssociationVO> enterpriseAssociationVOS = enterpriseAssociationWrapper.entityToVO(enterpriseAssociations);
//        List<PageData> pageDataList = new ArrayList<>();
//        try {
//            if (CollectionUtil.isNotEmpty(enterpriseAssociationVOS)) {
//                for (EnterpriseAssociationVO vo : enterpriseAssociationVOS) {
//                    PageData pd = new PageData();
//                    pd.put("id", vo.getId());
//                    pd.put("UNITNAME", vo.getOrgName());
//                    pd.put("APPLICANTNAME", vo.getCreateUser());
//                    pd.put("applicantId", vo.getCreateUser());
//                    pd.put("applyOrganizationId", vo.getOrgId());
//                    pd.put("formType", vo.getFormType());
//                    pd.put("orgName", vo.getOrgName());
//                    pageDataList.add(pd);
//                }
//            }
//            return pageDataList;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//        }
//        return pageDataList;
//    }
//
//    @Override
//    public List<Integer> getFormType() {
//        return Arrays.asList(DG_ENTERPRISE_ASSOCIATION);
//    }
//
//    /**
//     * 统一处理状态机事件方法
//     *
//     * @param id           主键
//     * @param updateStatus 更新状态
//     * @return boolean
//     */
//    private Boolean handleStatus(Long id, int updateStatus) {
//        try {
//            EnterpriseAssociation enterpriseAssociation = getById(id);
//            if (updateStatus == FormStatusConstant.FINISH) {
//                enterpriseAssociation.setApprovedUser(TokenUtil.getTokenUserId());
//                Date now = DateUtil.now();
//                enterpriseAssociation.setApprovedTime(now);
//            }
//            enterpriseAssociation.setStatus(updateStatus);
//            updateById(enterpriseAssociation);
//            return true;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            return false;
//        }
//    }
//}
